var wxpay = require('../../../utils/pay.js')
var app = getApp()
var ddapi = require('../../../utils/md5.js');
Page({
  data: {
    tabs: ["待付款(0)", "待使用(0)", "待评价(0)", "已完成(0)"],
    stv: {
      windowWidth: 0,
      lineWidth: 0,
      offset: 0,
      tStart: false
    },
    orderList: [[], [], [],[]],
    activeTab: 0,
  },
  onLoad: function (options) {
    try {
      let { tabs } = this.data;
      var res = wx.getSystemInfoSync()
      this.windowWidth = res.windowWidth;
      this.data.stv.lineWidth = this.windowWidth / this.data.tabs.length;
      this.data.stv.windowWidth = res.windowWidth;
      this.setData({ stv: this.data.stv })
      this.tabsCount = tabs.length;
    } catch (e) {
    }
  },
  onShow: function () {
    var that = this
    that.getOrderCount();
    that.getOrderList()
  },
  getOrderCount: function () {
    var that = this
    var service = 'User.GetOrderCount';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.GetOrderCount',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        if (res.data.data.code == 0) {
          var tabs = that.data.tabs;
          var info = res.data.data.info[0];
          console.log(info);
          if (info.one > 0) {
            tabs[0] = "待付款(" + info.one+")"
          } 
          if (info.tow > 0) {
            tabs[1] = "待使用(" + info.tow + ")"
          } 
          if (info.three > 0) {
            tabs[2] = "待评价(" + info.three + ")"
          } 
          if (info.fore > 0) {
            tabs[3] = "已完成(" + info.fore + ")"
          } 
          that.setData({
            tabs: tabs,
          });
        }
      }
    })
  },
  getOrderList: function () {
    var that = this
    var service = 'User.GetOrder';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var order_type = that.data.activeTab + 1;
    var sign = ddapi.md5(openid + order_type + service + user_id);
    var orderList = that.data.orderList;

    var sunList = orderList[that.data.activeTab];
    if (sunList.length == 0) {
      wx.request({
        url: app.globalData.apiUrl + '?service=User.GetOrder',
        header: {
          'Content-Type': 'json'
        },
        data: {
          sign: sign,
          open_id: openid,
          user_id: user_id,
          order_type: order_type
        },
        success: function (res) {
          console.log(res)
          if (res.data.data.code == 0) {
            var info = res.data.data.info;

            if (info.length > 0) {
              orderList[that.data.activeTab] = res.data.data.info
            }
            console.log(123);
            console.log(orderList);
            that.setData({
              orderList: orderList,
            });
          }
        }
      })
    }
  },

  handlerStart(e) {
    console.log('handlerStart')
    let { clientX, clientY } = e.touches[0];
    this.startX = clientX;
    this.tapStartX = clientX;
    this.tapStartY = clientY;
    this.data.stv.tStart = true;
    this.tapStartTime = e.timeStamp;
    this.setData({ stv: this.data.stv })
  },
  handlerMove(e) {
    console.log('handlerMove')
    let { clientX, clientY } = e.touches[0];
    let { stv } = this.data;
    let offsetX = this.startX - clientX;
    this.startX = clientX;
    stv.offset += offsetX;
    if (stv.offset <= 0) {
      stv.offset = 0;
    } else if (stv.offset >= stv.windowWidth * (this.tabsCount - 1)) {
      stv.offset = stv.windowWidth * (this.tabsCount - 1);
    }
    this.setData({ stv: stv });
  },
  handlerCancel(e) {

  },
  handlerEnd(e) {
    console.log('handlerEnd')
    let { clientX, clientY } = e.changedTouches[0];
    let endTime = e.timeStamp;
    let { tabs, stv, activeTab } = this.data;
    let { offset, windowWidth } = stv;
    //快速滑动
    if (endTime - this.tapStartTime <= 300) {
      console.log('快速滑动')
      //判断是否左右滑动(竖直方向滑动小于50)
      if (Math.abs(this.tapStartY - clientY) < 50) {
        //Y距离小于50 所以用户是左右滑动
        console.log('竖直滑动距离小于50')
        if (this.tapStartX - clientX > 5) {
          //向左滑动超过5个单位，activeTab增加
          console.log('向左滑动')
          if (activeTab < this.tabsCount - 1) {
            this.setData({ activeTab: ++activeTab })
          }
        } else if (clientX - this.tapStartX > 5) {
          //向右滑动超过5个单位，activeTab减少
          console.log('向右滑动')
          if (activeTab > 0) {
            this.setData({ activeTab: --activeTab })
          }
        }
        stv.offset = stv.windowWidth * activeTab;
      } else {
        //Y距离大于50 所以用户是上下滑动
        console.log('竖直滑动距离大于50')
        let page = Math.round(offset / windowWidth);
        if (activeTab != page) {
          this.setData({ activeTab: page })
        }
        stv.offset = stv.windowWidth * page;
      }
    } else {
      let page = Math.round(offset / windowWidth);
      if (activeTab != page) {
        this.setData({ activeTab: page })
      }
      stv.offset = stv.windowWidth * page;
    }
    stv.tStart = false;
    this.setData({ stv: this.data.stv })
  },
  ////////
  _updateSelectedPage(page) {
    console.log('_updateSelectedPage')
    let { tabs, stv, activeTab } = this.data;
    activeTab = page;
    this.setData({ activeTab: activeTab })
    stv.offset = stv.windowWidth * activeTab;
    this.setData({ stv: this.data.stv })
  },
  handlerTabTap(e) {
    console.log('handlerTapTap', e.currentTarget.dataset.index)
    this._updateSelectedPage(e.currentTarget.dataset.index);
  },
  //事件处理函数
  swiperchange: function (e) {
    var that = this
    //console.log('swiperCurrent',e.detail.current)
    let { tabs, stv, activeTab } = this.data;
    activeTab = e.detail.current;
    this.setData({ activeTab: activeTab })
    stv.offset = stv.windowWidth * activeTab;
    this.setData({ stv: this.data.stv })
    that.getOrderList()
  },
  toIndexPage: function () {
    wx.switchTab({
      url: "/pages/classification/index"
    });
  },
  pushdetail: function() {
    // wx.navigateTo({
    //   url: "/pages/ucenter/odetail/odetail"
    // });
  }
})